<?php
/*
 * 操作user_info表
 */
class UserMode extends Data{
    /** @return UserMode */
    public static function init() {
        return parent::init();
    }
    function add($user_id,$real_name = null,$hobbies=null,$birth=null,$idnum=null,$idnum_pic=null,
                 $point=0,$growth = 0,$lv_id =0, $money = 0, $service_id = null){
        $data['real_name']=$real_name;
        $data['hobbies']=$hobbies;
        $data['birth']=$birth;
        $data['idnum']=$idnum;
        $data['idnum_pic']=$idnum_pic;
        $data['point']=$point;
        $data['growth']=$growth;
        $data['lv_id']=$lv_id;
        $data['money']=$money;
        $data['user_id']=$user_id;
        $data['service_id']=$service_id;
        return $this->db->insert('user_info',$data)?(int)$this->db->insertId():-1;
    }
    //修改信息（only in user_info）
    function modify($id,$real_name = null,$hobbies=null,$birth=null,$idnum=null, $service_id = null,
                    $idnum_pic=null,$point=null,$growth = null,$lv_id =null, $money = null){
        $id=(int)$id;
        $data['real_name']=$real_name;
        $data['hobbies']=$hobbies;
        $data['birth']=$birth;
        $data['idnum']=$idnum;
        $data['idnum_pic']=$idnum_pic;
        $data['point']=$point;
        $data['growth']=$growth;
        $data['lv_id']=$lv_id;
        $data['money']=$money;
        $data['service_id']=$service_id;
        return $this->db->update('user_info',$id,$data) ==1;
    }
    
    //修改信息（only in user_info） 2015-08-31
    function modify_new($id,$data){
        return $this->db->update('user_info',$id,$data) ==1;
    }
    //获取用户信息
    function getDetail($user_id){
        $sql="SELECT UI.*,P.`url` as `idnum_url` FROM `user_info` UI LEFT JOIN `picture` P ON P.`id`=UI.`idnum_pic` WHERE UI.`user_id`=?";
        return $this->db->getOne($sql,$user_id);
    }
    function getDetailWithoutJoin($user_id){
        $sql="SELECT * FROM `user_info` WHERE `user_id`=?";
        return $this->db->getOne($sql,$user_id);
    }
    function getDetailInUser($user_id){
        $sql="SELECT * FROM `user` WHERE `id`=$user_id";
        return $this->db->getOne($sql,$user_id);
    }
    function getLvInfo($lv_id){
        $sql="SELECT * FROM `lv` WHERE `id`=?";
        return $this->db->getOne($sql,$lv_id);
    }
    //获得所有等级及要求
    function getAllLv($asc = true){
        $suffix = $asc ? ' ASC ':' DESC ';
        $sql="SELECT * FROM `lv` ORDER BY `limit`".$suffix;
        return $this->db->getAll($sql);
    }
    //修改用户等级
    function modifyLv($id,$lv_id){
        $data['lv_id']=$lv_id;
        return $this->db->update('user_info',$id,$data) == 1;
    }
    //用 成长值 判断等级
    function getLvIdByGrowth($growth){
        $growth=(int)$growth;
        $lvInfo=$this->getAllLv(false);
        foreach ($lvInfo as $v) {
            if($growth > $v['limit']){
                return $v['id'];
            }
        }
        return 1;
    }
    //下一等级积分要求
    function getNextLv($growth){
        $growth=(int)$growth;
        $lvInfo=$this->getAllLv(true);
        foreach ($lvInfo as $v) {
            if($growth < $v['limit']){
                return $v;
            }
        }
        return array();
    }
    /**
     * 修改成长值,顺便更新lv_id
     * @param $id
     * @param $growth
     * @param bool $add 增加
     * @return bool
     */
    function modifyGrowth($id,$growth,$add = false){
        $id=(int)$id;
        $growth=(int)$growth;
        $set=$add ? " `growth`+ ".$growth:$growth;
        $sql="UPDATE `user_info` SET `growth` = ".$set."  WHERE `id` = ?";
        if($this->db->sqlExec($sql,$id) == 1){
            $info=$this->getDetail($id);
            $lv_id=$this->getLvIdByGrowth($info['growth']);
            if($lv_id != $info['lv_id']){
                $this->modifyLv($info['id'],$lv_id);
            }
            return true;
        }
        return false;
    }
    //用户设置默认服务站
    function setDefaultService($id,$service_id){
        $data['service_id']=$service_id;
        return $this->db->update('user_info',$id,$data) == 1;
    }

    function getIdByUserId($user_id){
        $sql="SELECT `id` FROM `user_info` WHERE `user_id`=?";
        return $this->db->getValue($sql,$user_id);
    }
    function getIdByUsername($username){
        $sql="SELECT `id` FROM `user` WHERE `username`=?";
        return $this->db->getValue($sql,$username);
    }
    function  getBrowseList($user_id,$rows = 16,$offset = 0){
        $user_id=(int)$user_id;
        $rows=(int)$rows;
        $offset=(int)$offset;
        $sql="SELECT `gopen_id`,`id` as 'browse_id' FROM `browse_history` WHERE `user_id`=$user_id
              ORDER BY `create_time` DESC LIMIT $offset,$rows";
        return $this->db->getList($sql);
    }
    function addBrowse($user_id,$gopen_id){
        $browseH_id=$this->checkBrowseExist($user_id,$gopen_id);
        if($browseH_id>0){
            $data['create_time']=date('Y-m-d H:i:s',time());
            return $this->db->update('browse_history',$browseH_id,$data) == 1;
        }else{
            $data['user_id']=$user_id;
            $data['gopen_id']=$gopen_id;
            return $this->db->insert('browse_history',$data) == 1;
        }
    }
    function deleteBrowse($id=null,$user_id=null){
        if(!empty($id)){
            return $this->db->delete('browse_history',$id) == 1;
        }
        if(!empty($user_id)){
            $bIdsSql="SELECT `id` FROM `browse_history` WHERE `user_id`=?";
            $bIds=$this->db->getAll($bIdsSql,$user_id);
            $bIds=array_map(function($v){return $v['id'];},$bIds);
            $sql="DELETE FROM `browse_history` WHERE ".$this->db->getIdCondition($bIds,'id');
            return $this->db->sqlExec($sql,$user_id)>0;
        }
        return false;
    }
    function checkBrowseExist($user_id,$gopen_id){
        $user_id=(int)$user_id;
        $gopen_id=(int)$gopen_id;
        $sql="SELECT `id` FROM `browse_history`
              WHERE `user_id`=$user_id AND `gopen_id`=$gopen_id";
        return (int)$this->db->getValue($sql);
    }

    /**
     * 审核
     * @param $id
     * @param int $authPass 0|1
     * @return bool
     */
    function auth($id,$authPass = 1){
        $data['enable']=$authPass;
        return $this->db->update('user',$id,$data,true) == 1;
    }

    /**
     * 根据用户姓名查询用户信息
     */
    function getInfoByRealName($real_name){
        $real_name=$this->db->quote("%{$real_name}%");
        $sql="SELECT * FROM `user_info` WHERE `real_name` like ".$real_name;
        return $this->db->getOne($sql);
    }

    /**
     * 根据用户姓名获得用户id
     */
    function getUserIdByRealName($real_name){
        $real_name=$this->db->quote("%{$real_name}%");
        $sql="SELECT `user_id` FROM `user_info` WHERE `real_name` like ".$real_name;
        return $this->db->getValue($sql);
    }

    /**
     * 获得管理员信息列表
     */

    function getAdminList($rows = 16,$offset = 0){
        $rows=(int)$rows;
        $offset=(int)$offset;
        $sql="SELECT * FROM `admin` WHERE `enable`=1 ORDER BY `create_time` DESC LIMIT $offset,$rows";
        return $this->db->getList($sql);
    }
    /**
     * 获得单个管理员信息
     */
    function getAdminDetail($id){
        $sql="SELECT * FROM `admin` WHERE `enable`=1 AND `id`=$id";
        return $this->db->getOne($sql);
    }
    /**
     * 添加管理员
     */
    function adminAdd($username,$email,$code,$levels){
        $data['username']=$username;
        $data['email']=$email;
        $data['code']=$code;
        $data['levels']=$levels;
        $data['enable']=1;
        $data['type']=null;
        return $this->db->insert('admin',$data)?(int)$this->db->insertId():-1;
    }
    /**
     * 修改管理员信息
     */
    function adminModify($id,$levels){
        $data['levels']=$levels;
        $data['enable']=1;
        $data['type']=null;
        return $this->db->update('admin',$id,$data)==1;
    }
    /**
     * 删除管理员
     */
    function adminDelete($id){
        $sql="UPDATE `admin` SET `enable`= 0 WHERE `id`=?";
        return $this->db->sqlExec($sql,$id);
    }
    /**
     * 获得管理员权限字段
     */
    function getLevels($id){
        $sql="SELECT `levels` FROM `admin` WHERE `enable`=1 AND `id`=?";
        return $this->db->getValue($sql,(int)$id);
    }
    /**
     * 查看是否有相应权限
     */
    function checkLevels($level,$levels){
        return in_array($level,explode(",",$levels))?1:0;
    }
    /**
     * 根据id在admin表中查出是否是超级管理员
     */
    function getSuperById($id){
        $sql="SELECT * FROM `admin` WHERE `id`=?";
        return $this->db->getOne($sql,(int)$id);
    }

    //设置用户余额是否有用 id为user_info表id
    function setMoneyUsable($id,$usable = 1){
        $data['money_usable'] = $usable;
        return $this->db->update('user_info',$id,$data,true) == 1;
    }
    
    //获取系统的ID
    public function getplat_id(){
        $sql="select id from user where type='platsystem' ";
        return $this->db->getValue($sql);
    }
} 